/*******************************************************************
*
*    DESCRIPTION:gtkcallback.cpp implement the responsed function for signal
*
*    AUTHOR:LiuHui
*
*    HISTORY:
*
*    DATE:8/21/2006
*
*******************************************************************/
#include "head.h"
#include "outputmonitor.h"
#include "config.h"
#include "trayicon.h"
#include "outputmonitor.h"
#include "xmlutil.h"
#include "mainwindow.h"
/*
 *Function Name:on_allowping_button_clicked
 *
 *Parameters:
 *
 *Description:
 *
 *Returns:
 *
 */
void on_ping_button_clicked(GtkToggleButton *togglebutton, gpointer  user_data)
{

	char cmd[128];
	memset(cmd, 0, 128);
	gboolean is_active = gtk_toggle_button_get_active(togglebutton);
	if ( is_active )
	{
		//set xml file
		SetWetherAllowBePinged(conf_xml, 1);
		//add rules
		//INPUT
		sprintf(cmd, "iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT");
		system(cmd);
		//OUTPUT
		sprintf(cmd, "iptables -A OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT");
		system(cmd);
	}
	else
	{
		//set xml file
		SetWetherAllowBePinged(conf_xml, 0);
		//add rules
		//INPUT
		sprintf(cmd, "iptables -D INPUT -p icmp --icmp-type echo-request -j ACCEPT");
		system(cmd);
		//OUTPUT
		sprintf(cmd, "iptables -D OUTPUT -p icmp --icmp-type echo-reply -j ACCEPT");
		system(cmd);
	}
}

/*
 *Function Name:on_startup_button_clicked
 *
 *Parameters:
 *
 *Description:
 *
 *Returns:
 *
 */

void on_startup_button_clicked(GtkWidget *widget, gpointer user_data)
{

}

/*
 *Function Name:on_okbutton1_clicked
 *
 *Parameters:
 *
 *Description:the button in the welcome dialog
 *
 *Returns:void
 *
 */
void on_okbutton1_clicked(GtkWidget *widget, gpointer user_data)
{
	//mark the program has started once
	SetHaveStarted(conf_xml);
	//does user allow the firewall start with system boot?
	GtkWidget *checkbutton1 = glade_xml_get_widget(gui_xml, "checkbutton1");
	gboolean i = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbutton1));
	if ( i )//if user select start with boot
	{

		//set start with boot
		SetStartWithBoot(conf_xml);
		//copy autostart file
		system("cp ./coolnet.desktop $HOME/.config/autostart/");
	}
	else
	{
		CancelStartWithBoot(conf_xml);
	}
	xmlSaveFormatFile(CONFIG_PATH, conf_xml, 1);
	//load iptables modules
	system("modprobe iptable_filter");
	system("modprobe ip_queue");
	//init INPUT and OUTPUT chains
	system("iptables -F OUTPUT");
	system("iptables -F INPUT");
	system("iptables -A OUTPUT -p udp -j QUEUE");
	//create a thread to monitor the package
	pthread_t tid;
	pthread_create(&tid, NULL, &OutputMonitor, NULL);
	GtkWidget *dialog_welcome = glade_xml_get_widget(gui_xml, "dialog_welcome");
	gtk_widget_hide(dialog_welcome);

	//create notify icon
	GtkWidget *menu_notify = glade_xml_get_widget(gui_xml, "menu_notify");
	CreateTray(menu_notify);
}
/*
 *Function Name:on_deny_button_clicked
 *
 *Parameters:
 *
 *Description:
 *
 *Returns:
 *
 */
void on_denybutton_clicked(GtkWidget *widget, gpointer user_data)
{

	GtkWidget *dialog_promopt = glade_xml_get_widget(gui_xml, "dialog_promopt");
	gtk_widget_hide(dialog_promopt);
	GtkWidget *label_appname = glade_xml_get_widget(gui_xml, "label_appname");
	GtkWidget *label_packetid = glade_xml_get_widget(gui_xml, "label_packetid");
	char *appname = malloc(128);
	memset(appname, 0, 128);
	char *p = gtk_label_get_text(label_appname);
	strcpy(appname, p);

	app_conf[app_counter].allow = 0;
	app_conf[app_counter].name = appname;
	app_conf[app_counter].seqnum = ++app_counter;
	int i = AddAppName(conf_xml, 0, appname, ++xml_app_counter);
	//add to list_store
	GtkTreeIter iter;
	gtk_list_store_append(app_list_store, &iter);
	gtk_list_store_set(service_list_store, &iter, 0, appname, 2, FALSE, -1);
#ifdef _DEBUG
	printf("In on_denybutton_clicked:appname:%s app_counter:%d\n", appname, xml_app_counter);
#endif

	if ( i == 0 )
	{
		printf("Add app failed\n");
	}

	assert(app_counter < MAX_APP);
//dorp requeset packet
	unsigned long packetid = strtoul(gtk_label_get_text(GTK_LABEL(label_packetid)), NULL, 10);
#ifdef _DEBUG
	printf("In on_denybutton_clicked:packetid:---->%u\n", packetid);
#endif
	int status = ipq_set_verdict(h, packetid, NF_DROP, 0, NULL);
	if ( status < 0 )
	{
		printf("Packet:%d drop error\n", packetid);
	}
}
/*
 *Function Name:on_allowbutton_clicked
 *
 *Parameters:
 *
 *Description:
 *
 *Returns:
 *
 */
void on_allowbutton_clicked(GtkWidget *widget, gpointer user_data)
{
	GtkWidget *dialog_promopt = glade_xml_get_widget(gui_xml, "dialog_promopt");
	gtk_widget_hide(dialog_promopt);//hide the widget as soon as possible
	GtkWidget *label_appname = glade_xml_get_widget(gui_xml, "label_appname");
	GtkWidget *label_dport = glade_xml_get_widget(gui_xml, "label_dport");
	GtkWidget *label_packetid = glade_xml_get_widget(gui_xml, "label_packetid");
	GtkWidget *label_sport = glade_xml_get_widget(gui_xml, "label_sport");
	GtkWidget *label_protocol = glade_xml_get_widget(gui_xml, "label_protocol");
	GtkWidget *label_pid = glade_xml_get_widget(gui_xml, "label_pid");
	char *cname = malloc(128);
	memset(cname, 0, 128);
	int sport = atoi(gtk_label_get_text(GTK_LABEL(label_sport)));
	int dport = atoi(gtk_label_get_text(GTK_LABEL(label_dport)));
	int protocol;
	if ( strcmp("tcp", gtk_label_get_text(GTK_LABEL(label_protocol))) == 0 )
	{
		protocol = 6;
	}
	else
	{
		protocol = 17;
	}
	int pid = atoi(gtk_label_get_text(GTK_LABEL(label_pid)));

	char *p = gtk_label_get_text(GTK_LABEL(label_appname));
	strcpy(cname, p);
	//add to list_store
	GtkTreeIter iter;
	gtk_list_store_append(app_list_store, &iter);
	gtk_list_store_set(app_list_store, &iter, 0, cname, 2, TRUE, -1);
	//add to xml
	int i = AddAppName(conf_xml, 1, cname, ++xml_app_counter);
	if ( i == 0 )
	{
		printf("Add app failed\n");
	}
	assert(app_counter < MAX_APP);

	app_conf[app_counter].allow = 1;
	app_conf[app_counter].name = cname;
	app_conf[app_counter].seqnum = ++app_counter;

	assert(app_counter < MAX_APP);
	IptablesAllowOldApp(sport, dport, protocol, pid, app_counter - 1);
	unsigned long packetid = strtoul(gtk_label_get_text(GTK_LABEL(label_packetid)), NULL, 10);
#ifdef _DEBUG
	printf("packetid:---->%u\n", packetid);
#endif
	int status = ipq_set_verdict(h, packetid, NF_ACCEPT, 0, NULL);
	if ( status < 0 )
	{
		printf("Packet:%d drop error\n", packetid);
	}
}
/*
 *Function Name:on_tempbutton_clicked
 *
 *Parameters:
 *
 *Description:
 *
 *Returns:
 *
 */
void on_tempbutton_clicked(GtkWidget *widget, gpointer user_data)
{

	GtkWidget *dialog_promopt = glade_xml_get_widget(gui_xml, "dialog_promopt");
	gtk_widget_hide(dialog_promopt);
	GtkWidget *label_appname = glade_xml_get_widget(gui_xml, "label_appname");
	GtkWidget *label_dport = glade_xml_get_widget(gui_xml, "label_dport");
	GtkWidget *label_packetid = glade_xml_get_widget(gui_xml, "label_packetid");
	GtkWidget *label_sport = glade_xml_get_widget(gui_xml, "label_sport");
	GtkWidget *label_protocol = glade_xml_get_widget(gui_xml, "label_protocol");
	GtkWidget *label_pid = glade_xml_get_widget(gui_xml, "label_pid");
	char *cname = malloc(128);
	memset(cname, 0, 128);
	int sport = atoi(gtk_label_get_text(GTK_LABEL(label_sport)));
	int dport = atoi(gtk_label_get_text(GTK_LABEL(label_dport)));
	int pid = atoi(gtk_label_get_text(GTK_LABEL(label_pid)));

	int protocol;
	if ( strcmp("tcp", gtk_label_get_text(GTK_LABEL(label_protocol))) == 0 )
	{
		protocol = 6;
	}
	else
	{
		protocol = 17;
	}

	char *p = gtk_label_get_text(GTK_LABEL(label_appname));
	strcpy(cname, p);

	app_conf[app_counter].allow = 1;
	app_conf[app_counter].name = cname;
	app_conf[app_counter].seqnum = ++ app_counter;

	assert(app_counter < MAX_APP);
	IptablesAllowOldApp(sport, dport, protocol, pid, app_counter - 1);
	unsigned long packetid = strtoul(gtk_label_get_text(GTK_LABEL(label_packetid)), NULL, 10);

	int status = ipq_set_verdict(h, packetid, NF_ACCEPT, 0, NULL);
	if ( status < 0 )
	{
		printf("Packet:%d drop error\n", packetid);
	}
}

/*
 *Function Name:on_preferencemenu_active
 *
 *Parameters:
 *
 *Description:
 *
 *Returns:
 *
 */

void on_preference_menu_active(GtkWidget *widget, gpointer user_data)
{
#ifdef _DEBUG
	printf("enter preference menu callback function\n");
#endif
	GtkWidget *main_window = glade_xml_get_widget(gui_xml, "dialog_main");
	gtk_widget_show(main_window);
}


/*
 *Function Name:on_stopmenu_active
 *
 *Parameters:
 *
 *Description:
 *
 *Returns:
 *
 */

void on_stop_menu_active(GtkWidget *widget, gpointer user_data)
{
#ifdef _DEBUG
	printf("enter stop menu callback function\n");
#endif
	GtkWidget *stop_menu = glade_xml_get_widget(gui_xml, "imagemenuitem2");
	gtk_widget_hide(stop_menu);
	GtkWidget *active_menu = glade_xml_get_widget(gui_xml, "imagemenuitem6");
	gtk_widget_show(active_menu);

}

/*
 *Function Name:on_active_menu_active
 *
 *Parameters:
 *
 *Description:
 *
 *Returns:
 *
 */
void on_active_menu_active(GtkWidget *widget, gpointer user_data)
{

	GtkWidget *stop_menu = glade_xml_get_widget(gui_xml, "imagemenuitem2");
	gtk_widget_show(stop_menu);
	GtkWidget *active_menu = glade_xml_get_widget(gui_xml, "imagemenuitem6");
	gtk_widget_hide(active_menu);
}
